package com.itrident.loader.imlp;

import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.template.CustomLiveTemplateBase;
import com.intellij.codeInsight.template.impl.CustomLiveTemplateLookupElement;
import com.itrident.loader.AbstractLoader;

import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author cxx
 * @create 2020-12-29 10:48
 **/
public class FormatLoader extends AbstractLoader {

    private static final Pattern PATTERN =Pattern.compile ("^_p([idfbDs])([\\w]+)");

    private static final Map<String,String> PROPERTIES = new HashMap<> ();

    static {
        PROPERTIES.put ("i","Integer");
        PROPERTIES.put ("d","Double");
        PROPERTIES.put ("f","Float");
        PROPERTIES.put ("b", "BigDecimal");
        PROPERTIES.put ("D","Date");
        PROPERTIES.put ("s","String");
    }

    protected FormatLoader(CustomLiveTemplateBase ctb) {
        super (ctb);
    }

    @Override
    public List<LookupElement> load(String prefix) {
        List<LookupElement> list = new ArrayList<> (1);
        Matcher matcher = matcher (prefix);
        if(matcher.matches () && matcher.groupCount () == 2){
            String type = matcher.group (1);
            String val = PROPERTIES.get (type);
            String name = matcher.group (2);
            String text = "private "+val+" "+name+";";
            list.add (new CustomLiveTemplateLookupElement (ctb,text,prefix,text,false,true));
        }
        return list;
    }

    private Matcher matcher(String prefix) {
        return PATTERN.matcher (prefix);
    }
}
